from view.base import WordFrameBase, ReturnView
from qfluentwidgets import InfoBar, StateToolTip
from common import PdfToWordThread


class PdfToWordView(ReturnView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.frame = WordFrameBase()
        self.frame.setTitle('PDF转Word')
        self.frame.setLabelText('PDF文件路径：', 'Word文件路径：')

        self.stateToolTip = StateToolTip('PDF To Word', 'PDF正在转换Word，请稍后...', self)
        self.stateToolTip.hide()

        self.to_word_thread = PdfToWordThread(self)

        self.frame.started.connect(self.started_event)
        self.to_word_thread.started.connect(self.started_thread)
        self.to_word_thread.error.connect(self.error_thread)
        self.to_word_thread.finished.connect(self.finished_thread)

        self.vBox.addWidget(self.frame)

    def resizeEvent(self, event):
        self.stateToolTip.move(event.size().width() - self.stateToolTip.width() - 20, 10)
        super().resizeEvent(event)

    def started_event(self):
        file_path, output_path = self.frame.lineEditText()
        if file_path is False or output_path is False:
            return

        self.to_word_thread.set_value(file_path, output_path)
        self.to_word_thread.start()

    def started_thread(self):
        self.stateToolTip.show()

    def error_thread(self, msg):
        InfoBar.warning('错误', msg, duration=5000, parent=self)

    def finished_thread(self):
        self.stateToolTip.hide()
        InfoBar.success('提示', '转换完毕', duration=3000, parent=self)
